#!/bin/sh

httpport=""
serverport=""
ajpport=""
#这里定义webapps的目录，根据域名在此生成对应的目录
webappsdir="$(pwd)/App"
#这里定为配置文件的目录，根据域名生成对应的配置文件
websitedir="$(pwd)/tomcat"
#域名
domain=""
#Tomcat的Home目录
tomcathome="$(pwd)/apache-tomcat-7.0.62"
#上面的配置文件目录
templatedir="$(pwd)/template"
#访问路径，默认就是根目录，现在没用到
contextpath=""
#配置后是否直接运行
isrun="n"

echo "===========Input Port========="
#输入运行的端口号，每个实例都需要不同的端口号，我是以8181为基础，8182，8183 这样递增
read -p "(http run port):" httpport

if [ "$httpport" = "" ]; then
     echo "http port can't be empty"
     exit
fi
#server port 如果为空，则默认在上面的哪个端口中加个3，如：38181，38182
#read -p "(server port default 3$httpport):" serverport
if [ "$serverport" = "" ]; then
	serverport=3$httpport
fi
#ajport 和server port一样，运行端口千米加个4。如：48181
#read -p "(ajp port default 4$httpport):" ajpport
if [ "$ajpport" = "" ]; then
	ajpport=4$httpport
fi
#域名
echo "============input domain============="
read -p "domain:" domain
if [ "$domain" = "" ]; then
	echo " domain can't be empty"
	exit
fi

if [ -d "$websitedir/$domain" ]; then
        echo "==========================="
        echo "$domain is exist!"
        echo "==========================="      
	exit
fi

#如果不想用默认的webapps目录，则自定义输入，否则使用默认的+域名
#echo "============input webapps dir==========="
webappsdir_input=""
#read -p "webapps dir:(default $webappsdir/$domain):" webappsdir_input
if [ "$webappsdir_input" = "" ]; then
	webappsdir_input=$webappsdir/$domain
fi
#配置完后是否直接运行
#echo "=========is run now?(y/n)======="
#read -p "(default n):" isrun

#配置contextPath
echo "============input contextpath==========="
read -p "(contextPath default ROOT):" contextpath

if [ ! -e "$webappsdir_input" ]; then
	mkdir -p $webappsdir_input
fi

echo "http port: $httpport"
echo "server port: $serverport"
echo "ajp port: $ajpport"
echo "domain: $domain"
echo "webapps dir: $webappsdir_input"
echo "catalina base home: ${websitedir}/${domain}"
echo "======== Cp ========"
cp -r $templatedir/ $websitedir/$domain/ 
#以下均为替换 占位符
echo "=========replace======="
#echo $webappsdir_input

#tomcat.sh
sed -i "s#catalina_base_dir#${websitedir}/${domain}#g" `grep catalina_base_dir -rl $websitedir/$domain/bin/startup.sh`
sed -i "s#catalina_base_dir#${websitedir}/${domain}#g" `grep catalina_base_dir -rl $websitedir/$domain/bin/shutdown.sh`
sed -i "s#catalina_home_dir#${tomcathome}#g" `grep catalina_home_dir -rl $websitedir/$domain/bin/startup.sh`
sed -i "s#catalina_home_dir#${tomcathome}#g" `grep catalina_home_dir -rl $websitedir/$domain/bin/shutdown.sh`

#conf/server.xml
sed -i "s/server_port/${serverport}/g" `grep server_port -rl $websitedir/$domain/conf/server.xml`
sed -i "s/http_port/${httpport}/g" `grep http_port -rl $websitedir/$domain/conf/server.xml`
sed -i "s/ajp_port/${ajpport}/g" `grep ajp_port -rl $websitedir/$domain/conf/server.xml`

#ROOT.xml
if [ "$contextpath" = "" ]; then
	sed -i "s#webapps_dir#${webappsdir_input}#g" `grep webapps_dir -rl $websitedir/$domain/conf/Catalina/localhost/ROOT.xml`
	sed -i "s#context_path#${contextpath}#g" `grep context_path -rl $websitedir/$domain/conf/Catalina/localhost/ROOT.xml`
else
	mv $websitedir/$domain/conf/Catalina/localhost/ROOT.xml $websitedir/$domain/conf/Catalina/localhost/${contextpath}.xml
	sed -i "s#webapps_dir#${webappsdir_input}#g" `grep webapps_dir -rl $websitedir/$domain/conf/Catalina/localhost/${contextpath}.xml`
	sed -i "s#context_path#/${contextpath}#g" `grep context_path -rl $websitedir/$domain/conf/Catalina/localhost/${contextpath}.xml`
fi

#生成默认的index.html
if [ ! -f "$webappsdir_input/index.html" ]; then
	touch "$webappsdir_input/index.html"
	echo "<html><head><title>$domain</title></head><body>$domain</body><html>" > $webappsdir_input/index.html
fi


if [ "$isrun" = "y" ]; then
	sh $websitedir/$domain/tomcat.sh start
fi

#OK 
echo "=========Success============="
